home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 18017 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.7 KB

  1. Path: news.sprintlink.net!datalytics!usenet
  2. From: Rob Stewart <stew@datalytics.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Help! What's wrong with this?
  5. Date: Thu, 18 Apr 1996 11:32:54 -0400
  6. Organization: Datalytics, Inc
  7. Message-ID: <317660A6.56C7@datalytics.com>
  8. References: <4l22v3$1beo@useneta1.news.prodigy.com>
  9. NNTP-Posting-Host: 204.62.224.71
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (WinNT; I)
  14.  
  15. Brian Munroe wrote:
  16. > I'm trying to learn C++ and I've run into a strange problem in the
  17. > following code.
  18.  
  19. First, the code you submitted is C, not C++.  Just because 
  20. you're using a C++ compiler on it does not make it C++.  Let's 
  21. call it what it is.
  22.  
  23. > int const ADD_LEN = 40;
  24.  
  25. Try "const int ADD_LEN = 40" instead.
  26.  
  27. > int main()
  28. > {
  29. >   struct cust_list {
  30. >     char name[ADD_LEN];
  31. >     char add1[ADD_LEN];
  32. >     char add2[ADD_LEN];
  33. >     char add3[ADD_LEN];
  34. >   } customer;
  35. >   int flag;
  36. >   void get_customer_info(char temp[ADD_LEN], int flag);
  37.  
  38. This declaration should really come before main.
  39.  
  40. >   for (flag=0; flag<4; flag++)
  41. >   { switch (flag)
  42. >     { case 0: get_customer_info(customer.name, flag); break;
  43. >       case 1: get_customer_info(customer.add1, flag); break;
  44. >       case 2: get_customer_info(customer.add2, flag); break;
  45. >       case 3: get_customer_info(customer.add3, flag); break;
  46. >       default: exit(1);
  47. >     }
  48. >   }
  49.  
  50. This is the oddest thing I've ever seen.  Don't play games like 
  51. this, just write the following:
  52.  
  53.     get_customer_info(customer.name, 0);
  54.     get_customer_info(customer.add1, 1);
  55.     get_customer_info(customer.add2, 2);
  56.     get_customer_info(customer.add3, 3);
  57.  
  58. Having said that, it would be better for you to supply the 
  59. prompt string to get_customer_info rather than using a flag to 
  60. do it.  This allows the caller to more appopriately associate 
  61. the text of the prompt with the buffer being filled.
  62.  
  63. >   return 0;
  64. > }
  65. > void get_customer_info(char temp[ADD_LEN], int flag)
  66.  
  67. FYI: Declaring temp as char[ADD_LEN] doesn't guarrantee that you 
  68. get an array of that size.
  69.  
  70. > { int answer;
  71. >   int len;
  72. >   clrscr();
  73. >   switch (flag)
  74. >   { case 0:
  75. >      cout << "Enter the bidder's name in one of the following forms.\n";
  76. >      cout << "  Person: LAST NAME, FIRST NAME\n";
  77. >      cout << "  Business: COMPANY NAME\n\n";
  78. >      break;
  79. >     case 1:
  80. >      cout << "\nEnter first line of address\n";
  81. >      break;
  82. >     case 2:
  83. >      cout << "\nEnter second line of address\n";
  84. >      break;
  85. >     case 3:
  86. >      cout << "\nEnter third line of address\n";
  87. >      break;
  88. >     default:
  89. >      exit(1);
  90. >   }
  91. >   cin >> temp;
  92.  
  93. Try this instead.  It will eat whitespace before filling temp.
  94.  
  95.     cin >> ws >> temp;
  96.  
  97. >   len = strlen(temp);
  98. >   if (len < ADD_LEN)
  99. >    { strncat(temp, "                                        ",ADD_LEN-
  100. > len);}
  101. >   temp[ADD_LEN-1] = '\0';
  102.  
  103. Rather than have get_customer_info be dependent upon a global 
  104. value, why not pass the size of temp to it.  This reduces 
  105. dependence on the global to main, which is a better habit to 
  106. develop (later maintenance of code is easier with restricted 
  107. dependence on globals).
  108.  
  109. >   return;
  110. > }
  111. > When I run this all 4 prompts are outputted (Is that a word?), but only
  112. > input for the name and second line are  accepted. The input line doesn't
  113. > accept input for the first and third lines. I'm guessing that there is a
  114. > return character caught in the input buffer after I input the name and
  115. > second address lines and it's read in for the first and third lines. If
  116. > so, how do I get it out of the buffer and what the heck is it doing there
  117. > in the first place !?
  118. > Thanks for your help.
  119.  
  120. -- 
  121. Robert Stewart        | My opinions are usually my own.
  122. Datalytics, Inc.    | stew@datalytics.com
  123.